package com.bgybmi.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.jfinal.kit.JsonKit;
import com.jfinal.qyweixin.sdk.api.media.MediaApi;
import com.jfinal.qyweixin.sdk.api.media.MediaFile;

public class ApiUtil {

	/**
	 * 获取临时素材
	 */
	public static Boolean getMedia(String mediaId,String savePath) {
		Boolean flag=false;
		FileOutputStream fileOutputStream = null;
		BufferedOutputStream bufferedOutputStream = null;
		try {
			MediaFile media = MediaApi.getMedia(mediaId);
			BufferedInputStream fileStream = media.getFileStream();			
			fileOutputStream = new FileOutputStream(savePath+ media.getFullName());
			//fileOutputStream = new FileOutputStream("/Users/Javen/Documents/pic/test/" + media.getFullName());
			bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
			int bytesRead = 0;
			byte[] buff = new byte[1024];
			while (-1 != (bytesRead = fileStream.read(buff, 0, buff.length))) {
				bufferedOutputStream.write(buff, 0, bytesRead);
			}
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				bufferedOutputStream.flush();
				fileOutputStream.close();
				bufferedOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}
}
